<?php
namespace App\Model;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Agent extends BaseModel
{
    protected $connection = 'api';
    protected $userTable = 'user';
    protected $walletTable = 'finance_wallet';
    protected $userAgentTable = 'user_agent';
    protected $agentjStreamTable = 'finance_agentj_stream';
    protected $agentJdLogTable = 'agent_jd_log';
    protected $agentDiamondLogTable = 'agent_diamond_log';
    protected $diamondStreamTable = 'finance_diamond_stream';
    protected $userGolodAgentTable = 'user_agent_togold';
    protected $agentGolodLogTable = 'user_agent_togolod_log';
    //代理用户列表
    public function agentUserList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $userId = $request->input('user_id');
        $uuid = $request->input('uuid');

        $query = DB::connection($this->connection)->table($this->userAgentTable.' as a')
            ->leftJoin($this->userTable.' as b', "a.user_id", "=", "b.user_id")
            ->leftJoin($this->walletTable.' as c', "a.user_id", "=", "c.user_id")
            ->where('a.status', 1);
        if(!empty($userId)) {
            $query = $query->where('a.user_id', $userId);
        }
        if(!empty($uuid)) {
            $query = $query->where('b.uuid', $uuid);
        }
        $result = $query->paginate($limit, ['a.id', 'a.user_id', 'b.uuid', 'b.nickname', 'a.created_at', 'a.rate', 'c.agent_jd_balance'], 'page', $page);
        return $result;
    }

    //新增代理用户
    public function agentUserAdd($data){
        $res = curl_request(getKkaudioApiUrl() . '/inner/user/agentUserAdd', $data, 'json', 'POST');
        if(!isset($res['code'])) $res['code'] = 50000;
        if(!isset($res['msg'])) $res['msg'] = '系统异常';
        if(!isset($res['data'])) $res['data'] = [];
        return $res;
    }

    //删除代理用户
    public function agentUserDel($data){
        $res = curl_request(getKkaudioApiUrl() . '/inner/user/agentUserDel', $data, 'json', 'POST');
        if(!isset($res['code'])) $res['code'] = 50000;
        if(!isset($res['msg'])) $res['msg'] = '系统异常';
        if(!isset($res['data'])) $res['data'] = [];
        return $res;
    }

    //编辑代理用户
    public function agentUserEdit($data){
        $res = curl_request(getKkaudioApiUrl() . '/inner/user/agentUserEdit', $data, 'json', 'POST');
        if(!isset($res['code'])) $res['code'] = 50000;
        if(!isset($res['msg'])) $res['msg'] = '系统异常';
        if(!isset($res['data'])) $res['data'] = [];
        return $res;
    }

    //代理交易记录列表
    public function agentLogList(Request $request)
    {
        $page = !empty((int)$request->input('page')) && $request->input('page') >= 0 ? (int)$request->input('page') : 0;
        $limit = !empty((int)$request->input('limit')) && $request->input('limit') > 0 ? (int)$request->input('limit') : 10;
        $streamType = $request->input('agent_type');//交易类型 1=转出 2=兑换
        $timeSt = $request->input('time_st');//开始时间 字符串
        $timeEd = $request->input('time_ed');//结束时间 字符串
        $userId = $request->input('user_id');//代理用户ID
//        $query = DB::connection($this->connection)->table($this->agentjStreamTable)->orderBy('change_timestamp', 'desc');
//
        $query = DB::connection($this->connection)
            ->table('finance_diamond_stream')
            ->whereIn('diamond_type', [87,85])
            ->orderBy('change_timestamp', 'desc');
        if(!empty($userId)) {
            $query = $query->where('user_id', $userId);
        }
        if(!empty($streamType)) {
            if($streamType == 1) {
//                $query = $query->whereIn('agent_type', [51,52]);
                $query = $query->where('diamond_type', [87]);
            }
            if($streamType == 2) {
//                $query = $query->whereIn('agent_type', [1,2]);
                $query = $query->where('diamond_type', [85]);
            }
        }
        if(!empty($timeSt)) {
            $query = $query->where('change_timestamp', '>=', strtotime($timeSt));
        }
        if(!empty($timeEd)) {
            $query = $query->where('change_timestamp', '<=', strtotime($timeEd));
        }
        $result = $query->paginate($limit, ['*'], 'page', $page);

        $result = objToArray($result);
        foreach ($result['data'] as $k=>$v){
            $result['data'][$k]['diamond_type']=$v['diamond_type']==88?'兑换':'转出';
            $result['data'][$k]['change_timestamp']=date('Y-m-d H:i:s',$v['change_timestamp']);
        }
        if(!empty($result['data'])) {

        }
        return $result;
    }

    //代理钻石交易记录列表
    public function agentDiamondLogList(Request $request)
    {
        $page = !empty((int)$request->input('page')) && $request->input('page') >= 0 ? (int)$request->input('page') : 0;
        $limit = !empty((int)$request->input('limit')) && $request->input('limit') > 0 ? (int)$request->input('limit') : 10;
        $timeSt = $request->input('time_st');//开始时间 字符串
        $timeEd = $request->input('time_ed');//结束时间 字符串
        $from_user_id = $request->input('from_user_id');//转出用户ID
        $user_id = $request->input('user_id');//转入用户ID
        $query = DB::connection($this->connection)->table($this->agentDiamondLogTable.' as a')
            ->leftJoin($this->userTable.' as b', "a.user_id", "=", "b.user_id")
            ->leftJoin($this->userTable.' as c', "a.from_user_id", "=", "c.user_id")
            ->orderBy('a.created_at', 'desc');
        if(!empty($user_id)) {
            $query = $query->where('a.user_id', $user_id);
        }
        if(!empty($from_user_id)) {
            $query = $query->where('a.from_user_id', $from_user_id);
        }

        if(!empty($timeSt)) {
            $query = $query->where('a.created_at', '>=', date('Y-m-d H:i:s', strtotime($timeSt)));
        }
        if(!empty($timeEd)) {
            $query = $query->where('a.created_at', '<=', date('Y-m-d H:i:s', strtotime($timeEd)));
        }
        $result = $query->paginate($limit, ['a.id', 'a.user_id', 'b.uuid', 'b.nickname', 'a.from_user_id', 'c.uuid as from_uuid', 'c.nickname as from_nickname', 'a.diamond_num', 'a.agentd_type', 'a.created_at'], 'page', $page);
        $result = objToArray($result);
        if(!empty($result['data'])) {

        }
        return $result;
    }

    //代理金币用户列表
    public function agentGolodUserList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $userId = $request->input('user_id');
        $uuid = $request->input('uuid');
        $query = DB::connection($this->connection)->table($this->userGolodAgentTable.' as a')
            ->leftJoin($this->userTable.' as b', "a.user_id", "=", "b.user_id")
//            ->leftJoin($this->walletTable.' as c', "a.user_id", "=", "c.user_id")
            ->where('a.status', 1);
        if(!empty($userId)) {
            $query = $query->where('a.user_id', $userId);
        }
        if(!empty($uuid)) {
            $query = $query->where('b.uuid', $uuid);
        }
        $result = $query->paginate($limit, ['a.id', 'a.user_id', 'b.uuid', 'b.nickname', 'a.created_at', 'a.rate'], 'page', $page);
        return $result;
    }

    //新增金币代理用户
    public function agentGolodUserAdd($data){
        $res = curl_request(getKkaudioApiUrl() . '/inner/user/agentGolodUserAdd', $data, 'json', 'POST');
        if(!isset($res['code'])) $res['code'] = 50000;
        if(!isset($res['msg'])) $res['msg'] = '系统异常';
        if(!isset($res['data'])) $res['data'] = [];
        return $res;
    }
    //删除代理用户
    public function agentGolodUserDel($data){
        $res = curl_request(getKkaudioApiUrl() . '/inner/user/agentGolodUserDel', $data, 'json', 'POST');
        if(!isset($res['code'])) $res['code'] = 50000;
        if(!isset($res['msg'])) $res['msg'] = '系统异常';
        if(!isset($res['data'])) $res['data'] = [];
        return $res;
    }

    //编辑代理用户
    public function agentGolodUserEdit($data){
        $res = curl_request(getKkaudioApiUrl() . '/inner/user/agentGolodUserEdit', $data, 'json', 'POST');
        if(!isset($res['code'])) $res['code'] = 50000;
        if(!isset($res['msg'])) $res['msg'] = '系统异常';
        if(!isset($res['data'])) $res['data'] = [];
        return $res;
    }
    //代理交易记录列表
    public function agentGolodLogList(Request $request)
    {
        $page = !empty((int)$request->input('page')) && $request->input('page') >= 0 ? (int)$request->input('page') : 0;
        $limit = !empty((int)$request->input('limit')) && $request->input('limit') > 0 ? (int)$request->input('limit') : 10;
        $streamType = $request->input('agent_type');//交易类型 1=转出 2=兑换
        $timeSt = $request->input('time_st');//开始时间 字符串
        $timeEd = $request->input('time_ed');//结束时间 字符串
        $userId = $request->input('user_id');//代理用户ID
//        $query = DB::connection($this->connection)->table($this->agentjStreamTable)->orderBy('change_timestamp', 'desc');
//
        $query = DB::connection($this->connection)
            ->table('finance_kk_stream')
            ->whereIn('kk_type', [88,89])
            ->orderBy('change_timestamp', 'desc');
        if(!empty($userId)) {
            $query = $query->where('user_id', $userId);
        }
        if(!empty($streamType)) {
            if($streamType == 1) {
//                $query = $query->whereIn('agent_type', [51,52]);
                $query = $query->where('kk_type', [88]);
            }
            if($streamType == 2) {
//                $query = $query->whereIn('agent_type', [1,2]);
                $query = $query->where('kk_type', [89]);
            }
        }
        if(!empty($timeSt)) {
            $query = $query->where('change_timestamp', '>=', strtotime($timeSt));
        }
        if(!empty($timeEd)) {
            $query = $query->where('change_timestamp', '<=', strtotime($timeEd));
        }
        $result = $query->paginate($limit, ['*'], 'page', $page);

        $result = objToArray($result);
        foreach ($result['data'] as $k=>$v){
            
            $result['data'][$k]['kk_type']=$v['kk_type']==88?'兑换':'转出';
            $result['data'][$k]['change_timestamp']=date('Y-m-d H:i:s',$v['change_timestamp']);
        }
        if(!empty($result['data'])) {

        }
        return $result;
    }

    //代理钻石交易记录列表
    public function agentGolodKKLogList(Request $request)
    {
        $page = !empty((int)$request->input('page')) && $request->input('page') >= 0 ? (int)$request->input('page') : 0;
        $limit = !empty((int)$request->input('limit')) && $request->input('limit') > 0 ? (int)$request->input('limit') : 10;
        $timeSt = $request->input('time_st');//开始时间 字符串
        $timeEd = $request->input('time_ed');//结束时间 字符串
        $from_user_id = $request->input('from_user_id');//转出用户ID
        $user_id = $request->input('user_id');//转入用户ID
        $query = DB::connection($this->connection)->table($this->agentGolodLogTable.' as a')
            ->leftJoin($this->userTable.' as b', "a.user_id", "=", "b.user_id")
            ->leftJoin($this->userTable.' as c', "a.from_user_id", "=", "c.user_id")
            ->orderBy('a.created_at', 'desc');
        if(!empty($user_id)) {
            $query = $query->where('a.user_id', $user_id);
        }
        if(!empty($from_user_id)) {
            $query = $query->where('a.from_user_id', $from_user_id);
        }

        if(!empty($timeSt)) {
            $query = $query->where('a.created_at', '>=', date('Y-m-d H:i:s', strtotime($timeSt)));
        }
        if(!empty($timeEd)) {
            $query = $query->where('a.created_at', '<=', date('Y-m-d H:i:s', strtotime($timeEd)));
        }
        $result = $query->paginate($limit, ['a.id', 'a.user_id', 'b.uuid', 'b.nickname', 'a.from_user_id', 'c.uuid as from_uuid', 'c.nickname as from_nickname', 'a.diamond_num', 'a.agentd_type', 'a.created_at'], 'page', $page);
        $result = objToArray($result);
        if(!empty($result['data'])) {

        }
        return $result;
    }
}
